package me.keiwu.user.dao;

import me.keiwu.user.domain.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.List;

/**
 * Created by kei on 30/10/2017.
 */
public interface UserDao extends JpaRepository<User, Long>, JpaSpecificationExecutor<User> {

    User findByOpenId(String openId);

    @Query(value = "select * from `user` where `id` in (?1)", nativeQuery = true)
    List<User> getUsersByIdList(List<Long> idList);

//    @Query(value = "select * from `user` where (UNIX_TIMESTAMP(NOW()) - last_modified_time/1000)>=?1 and (UNIX_TIMESTAMP(NOW()) - last_modified_time/1000)<604800 and channel is null or channel <> 'robot'",
//            nativeQuery = true)
//    List<User> getInactiveUsersByTime(Long configTime);

    @Query(value = "select * from `user` where (UNIX_TIMESTAMP(NOW()) - last_modified_time/1000)>=432000 and (UNIX_TIMESTAMP(NOW()) - last_modified_time/1000)<604800 and channel is null or channel <> 'robot' \n-- #pageable\n",
    countQuery = "select count(*) from `user` where (UNIX_TIMESTAMP(NOW()) - last_modified_time/1000)>=432000 and (UNIX_TIMESTAMP(NOW()) - last_modified_time/1000)<604800 and channel is null or channel <> 'robot' ",
    nativeQuery = true)
    Page<User> getInactiveUsers(Pageable pageable);

}
